//
//  loginViewController.m
//  giuseppe
//
//  Created by Apple on 15/8/13.
//  Copyright (c) 2015年 GiantUmbrella. All rights reserved.
//

#import "loginViewController.h"
#import "schoolModel.h"
#import "userManager.h"
#import "userModel.h"


@interface loginViewController (){
    
    UITextField *mobileTextField;
    UITextField *passwordTextField;
    UITextField *securityCodeTextField;
    UITextField *schoolTextField;
    UITextField *studentNumberTextField;

    UIButton *resetPasswordButton;
    UIButton *gotAuthenticationCodeButton;
    UIButton *loginButton;
    UIButton *registerButton;

    NSMutableArray *schoolModelArray;
    schoolModel *schoolModel;
}

@end

@implementation loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = baseColor;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    
    //设置阴影
    [[navView layer] setShadowOffset:CGSizeMake(1, 1)];
    [[navView layer] setShadowRadius:2];
    [[navView layer] setShadowOpacity:1];
    [[navView layer] setShadowColor:[UIColor lightGrayColor].CGColor];
    
    [self _initView];
    
    [self.view addSubview:navView];

}

- (void)_initView{
    

    //logo图片
    UIImageView *logoImageView = [[UIImageView alloc]init];
    [logoImageView setImage:[UIImage imageNamed:@"loginLogo"]];
    [self.view addSubview:logoImageView];
    
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(220, 50));
        make.top.equalTo(self.view).with.offset(130);
        make.centerX.mas_equalTo(self.view);
        

    }];
    
    
    //输入框
    
//    UIView *mobileTextFieldView = [[UIView alloc]init];
    
    UIImageView *mobileImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mobile"]];
    
    mobileImageView.frame = CGRectMake(10, 100, 12, 18);
//    [self.view addSubview:mobileImageView];
    
    
    mobileTextField = [[UITextField alloc]initWithFrame:CGRectMake(kScreenWidth/2-kScreenWidth/5*3/2, kScreenHeight/3, kScreenWidth/5*3, 30)];
    mobileTextField.backgroundColor = [UIColor whiteColor];
    mobileTextField.keyboardType = UIKeyboardTypeNumberPad;
    mobileTextField.placeholder = @"手机号";
    
    
    passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(mobileTextField.frame.origin.x, mobileTextField.frame.origin.y+40, mobileTextField.frame.size.width, mobileTextField.frame.size.height)];
    passwordTextField.backgroundColor = [UIColor whiteColor];
    passwordTextField.secureTextEntry = YES;
    passwordTextField.placeholder = @"密码";
    
    securityCodeTextField = [[UITextField alloc]initWithFrame:CGRectMake(mobileTextField.frame.origin.x, mobileTextField.frame.origin.y+40, mobileTextField.frame.size.width, mobileTextField.frame.size.height)];
    securityCodeTextField.backgroundColor = [UIColor whiteColor];
    securityCodeTextField.secureTextEntry = YES;
    securityCodeTextField.placeholder = @"验证码";
    securityCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    gotAuthenticationCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [gotAuthenticationCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [gotAuthenticationCodeButton setTitleColor:mainColor forState:UIControlStateNormal];
    gotAuthenticationCodeButton.frame = CGRectMake(securityCodeTextField.frame.origin.x+securityCodeTextField.frame.size.width-160,5,60,20);
    gotAuthenticationCodeButton.titleLabel.font = [UIFont systemFontOfSize:11];
    [gotAuthenticationCodeButton addTarget:self action:@selector(gotAuthenticationCode) forControlEvents:UIControlEventTouchUpInside];
    [securityCodeTextField addSubview:gotAuthenticationCodeButton];
    

    schoolTextField = [[UITextField alloc]initWithFrame:CGRectMake(mobileTextField.frame.origin.x, mobileTextField.frame.origin.y+40, mobileTextField.frame.size.width, mobileTextField.frame.size.height)];
    schoolTextField.backgroundColor = [UIColor whiteColor];
    schoolTextField.secureTextEntry = YES;
    schoolTextField.placeholder = @"学校";
    
    studentNumberTextField = [[UITextField alloc]initWithFrame:CGRectMake(mobileTextField.frame.origin.x, mobileTextField.frame.origin.y+40, mobileTextField.frame.size.width, mobileTextField.frame.size.height)];
    studentNumberTextField.backgroundColor = [UIColor whiteColor];
    studentNumberTextField.secureTextEntry = YES;
    studentNumberTextField.placeholder = @"学号";
    
    
    resetPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [resetPasswordButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [resetPasswordButton setTitleColor:mainColor forState:UIControlStateNormal];
    resetPasswordButton.frame = CGRectMake(passwordTextField.frame.origin.x+passwordTextField.frame.size.width-60, passwordTextField.frame.origin.y+30, 60, 20);
    resetPasswordButton.titleLabel.font = [UIFont systemFontOfSize:11];
    [resetPasswordButton addTarget:self action:@selector(resetPassword) forControlEvents:UIControlEventTouchUpInside];
    

    
    
    [self.view addSubview:mobileTextField];
    [self.view addSubview:schoolTextField];
    [self.view addSubview:studentNumberTextField];
    [self.view addSubview:securityCodeTextField];
    [self.view addSubview:passwordTextField];
    [self.view addSubview:resetPasswordButton];
    
    
    //下方按钮
    
    loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.backgroundColor = mainColor;
    loginButton.frame = CGRectMake(20, kScreenHeight-150, kScreenWidth-40, 40);
    [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginButton.layer.cornerRadius = 3;
    [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.backgroundColor = mainColor;
    registerButton.tag = 101;
    registerButton.frame = CGRectMake(20, kScreenHeight-100, kScreenWidth-40, 40);
    [registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    registerButton.layer.cornerRadius = 3;
    [registerButton addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:loginButton];
    [self.view addSubview:registerButton];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}

- (void)resetPassword{
    
    [UIView animateWithDuration:0.3 animations:^(void){
        
        passwordTextField.frame =CGRectMake(mobileTextField.frame.origin.x, mobileTextField.frame.origin.y+40+40, mobileTextField.frame.size.width, mobileTextField.frame.size.height);
        
        resetPasswordButton.alpha = 0.00;
        
        loginButton.frame = CGRectMake(20, kScreenHeight-100, kScreenWidth-40, 40);
        loginButton.alpha = 0.00;
        
        [registerButton setTitle:@"找回密码" forState:UIControlStateNormal];
        registerButton.tag = 102;
        

    }completion:^(BOOL finish){
        
        [resetPasswordButton removeFromSuperview];
        [loginButton removeFromSuperview];
        
    }];
}

- (void)login{
    
    NSString *userName = mobileTextField.text;
    NSString *password = passwordTextField.text;

    
    //判断账号是否是合法手机号码
    if ([myDataAccess isMobileNumber:userName] == NO) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确手机号码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        
        [mobileTextField setText:@""];
        [passwordTextField setText:@""];
        
    }else{
        //判断是否有输入
        if (userName.length == 0 ||password.length == 0 ) {
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码为空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            [alertView show];
        }
        
        NSDictionary *paras = @{@"userName":userName,
                                 @"password":password,
                                 };

    //调用请求
    [[myDataAccess shardWebUtil] loginUsingWithURLString:kNetPath_Code_Login  andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
            
            NSDictionary *dic = [obj objectForKey:@"dataList"];
            [[userManager shardInfo].userArray addObject:dic];
            
        }else{

            
        }
        
        
    }];

    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
    }
}

- (void)getSchool{

    NSDictionary *paras = nil;
    
    //调用请求
    [[myDataAccess shardWebUtil] loginUsingWithURLString:kNetPath_Code_getSchoolList  andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
            NSArray *dataLiat = [obj objectForKey:@"dataList"];
            schoolModelArray = [[NSMutableArray alloc]init];
            for (NSDictionary *dic in dataLiat) {
                schoolModel *model = [schoolModel initWithDicAttributes:dic];
                [schoolModelArray addObject:model];
                NSLog(@"%@",schoolModelArray);

            }
        }
    }];

}


- (void)gotAuthenticationCode{
    
    NSString *phoneNumber = mobileTextField.text;

    //判断账号是否是合法手机号码
    if ([myDataAccess isMobileNumber:phoneNumber] == NO) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确手机号码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        
        [mobileTextField setText:@""];
        
    }else{
        //判断是否有输入
        if (phoneNumber.length == 0 ) {
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名为空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            [alertView show];
        }
        
        NSDictionary *paras = @{@"phoneNumber":phoneNumber,
                                };
        

    [[myDataAccess shardWebUtil] gettingCheckNumberUsingWithURLString:kNetPath_Code_gotAuthenticationCode  andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
            
            
            
        }else{
            
            
        }
        
        
    }];
    }
    
    
    [securityCodeTextField becomeFirstResponder];
    
}

- (void)registerAction{
    
    
    if (registerButton.tag == 101) {
        [UIView animateWithDuration:0.3 animations:^(void){
            
            passwordTextField.frame =CGRectMake(mobileTextField.frame.origin.x, mobileTextField.frame.origin.y+40+40, mobileTextField.frame.size.width, mobileTextField.frame.size.height);
            
            
            schoolTextField.frame =CGRectMake(mobileTextField.frame.origin.x, mobileTextField.frame.origin.y+40+40+40, mobileTextField.frame.size.width, mobileTextField.frame.size.height);
            
            
            
            studentNumberTextField.frame =CGRectMake(mobileTextField.frame.origin.x, mobileTextField.frame.origin.y+40+40+80, mobileTextField.frame.size.width, mobileTextField.frame.size.height);
            
            
            resetPasswordButton.alpha = 0.00;
            
            loginButton.frame = CGRectMake(20, kScreenHeight-100, kScreenWidth-40, 40);
            loginButton.alpha = 0.00;
            
            
            
        }completion:^(BOOL finish){
            
            [resetPasswordButton removeFromSuperview];
            [loginButton removeFromSuperview];
            
        }];

    }else{
        
        
        NSString *userName = mobileTextField.text;
        NSString *authenticationCode = securityCodeTextField.text;
        NSString *newPassword = passwordTextField.text;
        
        //判断账号是否是合法手机号码
        if ([myDataAccess isMobileNumber:userName] == NO) {
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确手机号码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            
            [mobileTextField setText:@""];
            [mobileTextField becomeFirstResponder];
            
        }else{
            //判断是否有输入
            if (userName.length == 0 ||authenticationCode.length == 0||newPassword.length == 0) {
                
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户或验证码或密码为空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                [alertView show];
            }
            
            NSDictionary *paras = @{@"userName":userName,
                                    @"authenticationCode":authenticationCode,
                                    @"newPassword":newPassword
                                    };
            
            
            [[myDataAccess shardWebUtil] gettingCheckNumberUsingWithURLString:kNetPath_Code_gotAuthenticationCode  andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
                
                NSLog(@"%@",obj);
                
                if (obj) {
                    
                    
                    
                }else{
                    
                    
                }
                
                
            }];
        }
        

        
        
        
        
    }


}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
